package analyzemytime.reports;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import analyzemytime.datamodel.Employee;
import analyzemytime.datamodel.Project;
import analyzemytime.datamodel.Report;
import analyzemytime.utils.PrintReport;

public class MonthlyProjectReport {
	HashMap<Employee,Double> monthlyProjectReportMap;
	HashMap<Project, ArrayList<Report>> reportsByProject;
	private HashMap <Project,HashMap<Employee,Double>>monthlyEmployeeOverallReportMap = new HashMap<Project, HashMap<Employee,Double>>();
	
	public HashMap<Project, HashMap<Employee, Double>> getMonthlyEmployeeOverallReportMap() {
		return monthlyEmployeeOverallReportMap;
	}

	public void setMonthlyEmployeeOverallReportMap(
			HashMap<Project, HashMap<Employee, Double>> monthlyEmployeeOverallReportMap) {
		this.monthlyEmployeeOverallReportMap = monthlyEmployeeOverallReportMap;
	}

	public MonthlyProjectReport(HashMap<Project, ArrayList<Report>> reportsAggregatedByProject) {
		this.monthlyProjectReportMap=new HashMap<Employee,Double>();
		this.reportsByProject=reportsAggregatedByProject;
	}
	
	public void generateMonthlyProjectReport(String projectName, String month, String year)
	{
		Iterator<Project> monthlyReportByProjectIterator=reportsByProject.keySet().iterator();
		while(monthlyReportByProjectIterator.hasNext())
		{
			Project key = monthlyReportByProjectIterator.next();
			ArrayList<Report> reportList=reportsByProject.get(key);
			for(Report report: reportList)//Employee employee,Project project,String date, double reportedTime
			{
				if(report.getProject().getProjectName().equals(projectName))//if we have the project we're looking for
				{
					if(report.getDate().getYear() == Integer.parseInt(year))//if we have the year we're looking for
					{
						if(report.getDate().getMonthOfYear() == Integer.parseInt(month))//if we have the month we're looking for
						{
							Employee mapKey = report.getEmployee();
							Double mapValue = report.getTimeReported();
							if(!monthlyProjectReportMap.containsKey(mapKey))//if we don't have a key for this employee yet
							{
								monthlyProjectReportMap.put(mapKey,mapValue);
							}
							else//if we do then we should update his total reported time for the project
							{
								monthlyProjectReportMap.put(mapKey,monthlyProjectReportMap.get(mapKey)+mapValue);
							}
						}
					}
				}
			}
		}
		

	}
	
	public void generateMonthlyProjectOverallReport(String month,String year)
	{
		Iterator<Project> monthlyReportOverallIterator=reportsByProject.keySet().iterator();
		while(monthlyReportOverallIterator.hasNext())
		{
			Project key = monthlyReportOverallIterator.next();
			HashMap<Employee,Double> tempMap = new HashMap<Employee, Double>();
			ArrayList<Report> reportList=reportsByProject.get(key);
			for(Report report: reportList)//Employee employee,Project project,String date, double reportedTime
			{
				if(report.getDate().getYear() == Integer.parseInt(year) && report.getDate().getMonthOfYear() == Integer.parseInt(month))//if we have the year we're looking for
				{
						Employee mapKey = report.getEmployee();
						Double mapValue = report.getTimeReported();
						if(!tempMap.containsKey(mapKey))
						{
							tempMap.put(mapKey,mapValue);
						}
						else
						{
							tempMap.put(mapKey,tempMap.get(mapKey)+mapValue);//previous value + current value
						}
				}
				
			}
			System.out.println("");
			monthlyEmployeeOverallReportMap.put(key, tempMap);
		}
	}
	
	public void printMonthlyProjectReportToConsole(String projectName,String month,String year)
	{
		System.out.println("Project: "+projectName+" Month: "+month+" Year: "+year);
		System.out.println("Employee | hours");
		Iterator<Employee> monthlyProjectReportIterator=monthlyProjectReportMap.keySet().iterator();
		while(monthlyProjectReportIterator.hasNext())//we need to go through every map key and print it along with its value to the console
		{
			Employee employee=monthlyProjectReportIterator.next();
			System.out.println(employee.toString()+"    "+monthlyProjectReportMap.get(employee));
		}		
	}

	
	public void printMonthlyProjectReportToFile(String projectName,String month,String year)
	{
		String content = "";
		
		content += "Project,"+projectName+",Month,"+month+",Year,"+year+"\n\r";
		content += "Employee,hours\n\r";
		Iterator<Employee> monthlyProjectReportIterator=monthlyProjectReportMap.keySet().iterator();
		while(monthlyProjectReportIterator.hasNext())//we need to go through every map key and print it along with its value to the console
		{
			Employee employee=monthlyProjectReportIterator.next();
			content += employee.toString()+","+monthlyProjectReportMap.get(employee)+"\n\r";
		}
		
		PrintReport.PrintToFile(content, year+"_"+month+"_"+projectName);
	}
	
	//TODO
	public void drawMonthlyProjectReportGraph()
	{
		
	}
}
